define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme','form', ,'template'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Form,Template) {

    var Controller = {
        index: function () {
            Controller.api.bindevent();
            Form.api.bindevent($('.panel-body'),function () {

            });

            var url = 'dashboard/index';
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');
            //绑定事件
            $('a[data-toggle="tab"]').on('click', function (e) {
                var time_type = $(this).data('value');
                if(time_type == 6){
                    $("#create_div").removeClass('hidden');
                    return;
                }else{
                    if(!$("#create_div").hasClass('hidden')){
                        $("#create_div").addClass('hidden');
                    }
                }
                $("#createtime").val('');
                getCompanyInfo(time_type);
            });

            $("#createtime_button").on('click', function(){
                getCompanyInfo(6);
            });

            var statistics = {
                column : [],
                usersStatics: [],
                vistStatics: [],
                niuStatics: [],
                giftStatics: []
            };
            var timer = null;
            function getCompanyInfo(time_type){
                var time_data = $("#createtime").val();
                Fast.api.ajax({
                    url: url,
                    data: {
                        time_type: time_type,
                        time_data : time_data
                    }
                }, function (ret) {
                    statistics = {
                        column : [],
                        usersStatics: [],
                        vistStatics: [],
                        niuStatics: [],
                        giftStatics: []
                    };
                    statistics = ret.statistics;
                    var rightData = ret.rightData;
                    var zh = ret.type_zh;
                    $("#re_value").text(rightData.register);
                    $("#re_key").text(zh + '注册');
                    $("#login_value").text(rightData.login);
                    $("#login_key").text(zh + '登陆');

                    $("#niu_value").text(rightData.niu);
                    $("#niu_key").text(zh + '牛总单');
                    $("#niuno_value").text(rightData.niuno);
                    $("#niuno_key").text(zh + '牛未处理');

                    $("#gift_value").text(rightData.gift);
                    $("#gift_key").text(zh + '礼品总单');
                    $("#giftno_value").text(rightData.giftno);
                    $("#giftno_key").text(zh + '礼品未处理');
                    initChart();
                    return false;
                });
            }

            // 基于准备好的dom，初始化echarts实例
            function getchartdata(statistics){

                // 指定图表的配置项和数据
                var option = {
                    title: {
                        text: '',
                        subtext: ''
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: [ "会员数",'访问量', '牛订单', '礼品订单']
                    },
                    toolbox: {
                        show: false,
                        feature: {
                            magicType: {show: true, type: ['stack', 'tiled']},
                            saveAsImage: {show: true}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: statistics.column
                    },
                    yAxis: {},
                    grid: [{
                        left: 'left',
                        top: 'top',
                        right: '10',
                        bottom: 30
                    }],
                    series: [{
                        name: '会员数',
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: statistics.usersStatics
                    },
                        {
                            name: '访问量',
                            type: 'line',
                            smooth: true,
                            areaStyle: {
                                normal: {}
                            },
                            lineStyle: {
                                normal: {
                                    width: 1.5
                                }
                            },
                            data: statistics.vistStatics
                        },

                        {
                            name: '牛订单',
                            type: 'line',
                            smooth: true,
                            areaStyle: {
                                normal: {}
                            },
                            lineStyle: {
                                normal: {
                                    width: 1.5
                                }
                            },
                            data: statistics.niuStatics
                        },
                        {
                            name: '礼品订单',
                            type: 'line',
                            smooth: true,
                            areaStyle: {
                                normal: {}
                            },
                            lineStyle: {
                                normal: {
                                    width: 1.5
                                }
                            },
                            data: statistics.giftStatics
                        }
                    ]
                };

                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);

                if ($("#echart").width() != $("#echart canvas").width() && $("#echart canvas").width() < $("#echart").width()) {
                    myChart.resize();
                }
            }

            //echart实例
            function initChart() {
                //清楚定时任务
                window.clearInterval(timer);
                var column = [];
                var usersStatics = [];
                var vistStatics = [];
                var niuStatics = [];
                var giftStatics = [];
                timer = window.setInterval(function () {
                    if (column.length === 0) {
                        column = $.merge(column, statistics.column);
                    }
                    if (usersStatics.length === 0) {
                        usersStatics = $.merge(usersStatics, statistics.usersStatics);
                    }
                    if (vistStatics.length === 0) {
                        vistStatics = $.merge(vistStatics, statistics.vistStatics);
                    }
                    if (niuStatics.length === 0) {
                        niuStatics = $.merge(niuStatics, statistics.niuStatics);
                    }
                    if (giftStatics.length === 0) {
                        giftStatics = $.merge(giftStatics, statistics.giftStatics);
                    }

                    //向头部插入一条数据
                    statistics.column.push(column.shift());
                    statistics.usersStatics.push(usersStatics.shift());
                    statistics.vistStatics.push(vistStatics.shift());
                    statistics.niuStatics.push(niuStatics.shift());
                    statistics.giftStatics.push(giftStatics.shift());

                    //按自己需求可以取消这个限制
                    //移除最开始的一条数据
                    statistics.column.shift();
                    statistics.usersStatics.shift();
                    statistics.vistStatics.shift();
                    statistics.niuStatics.shift();
                    statistics.giftStatics.shift();

                    getchartdata(statistics);
                }, 3000);
            }

            getCompanyInfo(0);
            $(window).resize(function () {
                myChart.resize();
            });

            $(document).on("click", ".btn-checkversion", function () {
                top.window.$("[data-toggle=checkupdate]").trigger("click");
            });
            $(window).resize(function () {
                myChart.resize();
            });

            $(document).on("click", ".btn-checkversion", function () {
                top.window.$("[data-toggle=checkupdate]").trigger("click");
            });

            $(document).on("click", ".btn-refresh", function () {
                setTimeout(function () {
                    myChart.resize();
                }, 0);
            });

        },

        api: {
            bindevent: function () {

            }
        }
    };

    return Controller;
});